#!/bin/bash
# TEAMBOX INSTALLATION ON UBUNTU 11.04 SERVER FRESH INSTALL
# @author Sylvain Lévesque <slevesque@gezere.com>
# @license GPLv3

# APACHE CONFIGURATION
APACHE_SERVER_NAME=servername.domainname.com
APACHE_DEFAULT_SITE=/etc/apache2/sites-available/default
APACHE_CONF=/etc/apache2/httpd.conf
APACHE_VHOST_NAME=teambox.domainname.com

# PASSENGER PHUSION CONFIGURATION
PASSENGER_CONF=/etc/apache2/mods-available/passenger.conf
PASSENGER_LOAD=/etc/apache2/mods-available/passenger.load

# MYSQL CONFIGURATION
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=yourpassword
MYSQL_DATABASE=teambox

# SMTP CONFIGURATION
SMTP_HOST=smtp.domainname.com
SMTP_AUTH=plain
SMTP_USERNAME=yourusername@username.com
SMTP_PASSWORD=yourpassword
SMTP_PORT=25
SMTP_ENABLE_STARTTLS_AUTO=false

# RAILS CONFIGURATION
RAILS_ENV=production

# TEAMBOX CONFIGURATION
TEAMBOX_INSTALLATION_PATH=/var/www
TEAMBOX_PATH="$TEAMBOX_INSTALLATION_PATH"/teambox


####
# DON'T CHANGE ANYTHING BELLOW THIS LINE
####

# System updates
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
apt-get clean
apt-get autoclean

# Install required packages
sudo apt-get install \
  build-essential ruby ruby-dev irb libmysqlclient15-dev sqlite3 libsqlite3-dev \
  libcurl4-openssl-dev libopenssl-ruby libpcre3-dev libxml2-dev libxslt-dev \
  libreadline5-dev apache2 apache2-prefork-dev libapr1-dev imagemagick \
  mysql-server-5.1 bsd-mailx postfix rubygems \
  git ruby

# Update rubygems
export REALLY_GEM_UPDATE_SYSTEM=1
gem update --system
gem install rubygems-update
update_rubygems

# Install bundler
sudo gem install bundler

# Install rails
gem install rails

# Checkout teambox
cd "$TEAMBOX_INSTALLATION_PATH"
git clone git://github.com/teambox/teambox.git
cd "$TEAMBOX_PATH"
git checkout master

# Install required gems
bundle install --deployment

# Configure database

echo "Configure your database.yml file..."
echo "You can copy and paste the following lines."
echo
echo "production:"
echo "  adapter: mysql"
echo "  host: $MYSQL_HOST"
echo "  username: $MYSQL_USER"
echo "  password: $MYSQL_PASSWORD"
echo "  database: $MYSQL_DATABASE"
echo
echo "Press any key to continue..."
read answer

cp "$TEAMBOX_PATH"/config/database.example.yml "$TEAMBOX_PATH"/config/database.yml
vi "$TEAMBOX_PATH"/config/database.yml

# Configure teambox config file.
echo "Configure your teambox.yml file."
echo "You can copy and paste the following lines."
echo
echo "domain: $APACHE_VHOST_NAME"
echo 
echo "host: $SMTP_HOST"
echo "username: $SMTP_USERNAME"
echo "password: $SMTP_PASSWORD"
echo "auth: $SMTP_AUTH"
echo "port: $SMTP_PORT"
echo "enable_starttls_auto: $SMTP_ENABLE_STARTTLS_AUTO"
echo
echo "Press any key to continue..."
read answer

vi "$TEAMBOX_PATH"/config/teambox.yml

# Bootstrap de database
bundle exec rake db:create db:schema:load RAILS_ENV="$RAILS_ENV"

# Set rights
chown -R www-data.www-data "$TEAMBOX_PATH"
chmod -R 755 "$TEAMBOX_PATH"

# Testing if it works !
#0:0:0:0:3000
#rails server -e "$RAILS_ENV"

# SPHINX SEARCH
#install from apt-get
#apt-get install sphinxsearch
#enable auto start with: START=yes
#vi /etc/default/sphinxsearch
#install sphinx gem
#gem install sphinx
#gem install rake
#run the following from the base to generate the files you need
#bundle exec rake ts:index RAILS_ENV=production
#bundle exec rake ts:start RAILS_ENV=production
#link the config to auto run without rake starting it up each time
#ln -s web/config/production.sphinx.conf /etc/sphinxsearch/sphinx.conf
#kill sphinxsearch which rake started (ps aux | grep sphinx) then start the daemon
#service sphinxsearch start
#continue to rebuilding and reindexing below

#Enabling search

#By default search in Teambox is disabled. You need to modify “config/teambox.yml” in order to enable it:

# Enable search (you must have thinking_sphinx installed and configured)
#allow_search: false

#Once you have enabled search, you need to generate the search index:

#bundle exec rake ts:index RAILS_ENV=production

#Then simply run the following to start sphinx:

#bundle exec rake ts:start RAILS_ENV=production

#Finally, you will need to periodically update the search index (especially after updating Teambox sources) in order to keep up to date:

#bundle exec rake ts:rebuild RAILS_ENV=production

#Configuring the indexer

#You should add a line to your crontab to reindex the database using Sphinx. Something like this would reindex your database every 5 minutes:

#*/5 * * * * cd /path/to/teambox && RAILS_ENV=production /usr/bin/env bundle exec rake ts:reindex >> log/cron.log 2>&1



# APACHE PHUSION
sudo gem install passenger
sudo passenger-install-apache2-module

#Dans la config d'apache.
touch $PASSENGER_CONF
echo "PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-3.0.8" >> $PASSENGER_CONF
echo "PassengerRuby /usr/bin/ruby1.8" >> $PASSENGER_CONF
touch $PASSENGER_LOAD
echo "LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-3.0.8/ext/apache2/mod_passenger.so" >> $PASSENGER_LOAD

#Enable mod_passenger and other modules
a2enmod passenger vhost_alias rewrite headers deflate expires

# Setting server name
echo "ServerName $APACHE_SERVER_NAME" >> $APACHE_CONF

# Create vhost
echo "<VirtualHost *:80>" >> $APACHE_DEFAULT_SITE
echo "  ServerName $APACHE_VHOST_NAME" >> $APACHE_DEFAULT_SITE
echo '  DocumentRoot "/var/www/teambox/public"' >> $APACHE_DEFAULT_SITE
echo '  <Directory "/var/www/teambox/public">' >> $APACHE_DEFAULT_SITE
echo '    AllowOverride all' >> $APACHE_DEFAULT_SITE
echo '    Options -MultiViews' >> $APACHE_DEFAULT_SITE
echo '    Order allow,deny' >> $APACHE_DEFAULT_SITE
echo '    Allow from all' >> $APACHE_DEFAULT_SITE
echo '  </Directory>' >> $APACHE_DEFAULT_SITE
echo '  RailsEnv production' >> $APACHE_DEFAULT_SITE
echo '</VirtualHost>' >> $APACHE_DEFAULT_SITE

# Restart apache
sudo /etc/init.d/apache2 restart
